package com.xpffly.springboot.config.db.druid;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.xpffly.springboot.config.db.druid.annotation.DataSourceNames;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xpengfei
 * @Date: 2020/12/9 21:41
 * @Description：druid 多数据源配置
 */
@Configuration
public class DruidDynamicDataSourceConfig {


    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 配置数据源 stock
     *
     * @return
     */
    @Bean(name = "stock")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.druid.stock")
    public DataSource stockDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 配置数据源 house
     *
     * @return
     */
    @Bean(name = "house")
    @ConfigurationProperties(prefix = "spring.datasource.druid.house")
    public DataSource houseDataSource() {
        return DruidDataSourceBuilder.create().build();
    }


    /**
     * 设置默认数据源及数据源集合
     *
     * @return
     */
    @Bean
    @Primary
    public DataSource multipleDataSource() {
        Map<Object, Object> targetDataSources = new HashMap<>(5);
        targetDataSources.put(DataSourceNames.STOCK_DB, stockDataSource());
        targetDataSources.put(DataSourceNames.HOUSE_DB, houseDataSource());
        return new DruidDynamicDataSource(stockDataSource(), targetDataSources);
    }



    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        // 在这里注入sqlSessionFactory要使用的数据源，而这里获取到的数据源正是我们实现的AbstractRoutingDataSource类所返回的数据源
        sqlSessionFactory.setDataSource(multipleDataSource());
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        sqlSessionFactory.setConfiguration(configuration);
        //添加分页功能
        sqlSessionFactory.setPlugins(paginationInterceptor());
        return sqlSessionFactory.getObject();
    }
}
